package com.example.quartz.jobbuilder;

import com.alibaba.fastjson.JSON;
import com.example.quartz.entity.JobInfo;
import com.example.quartz.entity.TriggerInfo;
import com.example.quartz.myjob.MyJob;
import com.example.quartz.trigger.MyTrigger;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class MyJobBuilder<T> {
    @Autowired
    private Scheduler scheduler;


    public JobDetail create1(JobInfo jobInfo, T jobData) {
        return JobBuilder.newJob(MyJob.class)
                .usingJobData(jobInfo.getJobName(), JSON.toJSONString(jobData))
                .withIdentity(jobInfo.getJobName(), jobInfo.getJobGroup())
                .build();
    }

    public void register(JobInfo jobInfo, TriggerInfo triggerInfo, T jobData) throws SchedulerException {
        scheduler.scheduleJob(create1(jobInfo, jobData), MyTrigger.create1(triggerInfo));
//        scheduler.scheduleJob(create2(), MyTrigger.create2());
    }
}
